let userTel =document.querySelector(".userTel")
let telSelect=document.querySelector('.telSelect');
telSelect.onmouseover = function(e) {
  telSelect.style.backgroundColor ="#e6e6e6"
}
telSelect.onmouseout = function(e) {
  telSelect.style.backgroundColor ="rgb(255,255,255)"

}
let telSelectItem=document.querySelector(".telSelectItem")
let arr=[]
async function selectItem(){
  let obj=await axios.get(telSelectAPI)
  console.log(obj);
  arr=obj.data.Data;
  console.log(arr);
  // CountryName: '中国大陆', EnglishName: 'China', Prefix: 86, 
  let itemList=arr.map((v)=>{
    return `
    <div class="sItem" onclick="item(${v.ID})">
                <span>${v.CountryName}</span>
                <span> +${v.Prefix}</span>
              </div>`
  })
  telSelectItem.innerHTML = itemList.join('')
}
selectItem()
telSelect.onclick=function(){
  telSelectItem.style.display = 'block';
}


userTel.onclick=function(){
  telSelectItem.style.display = 'none';

}
let userPW=document.querySelector(".userPW")
userPW.onclick=()=>telSelectItem.style.display = 'none';

function item(Id){
  console.log(arr);
  console.log(Id);
  telSelectItem.style.display = 'none';
  let itemSel=arr.find((v)=>{    return v.ID ==Id;  })
  telSelect.innerHTML= `+${itemSel.Prefix}`
  console.log(itemSel);
}
let autoSta=""
let autoLogin=document.querySelector(".autoLogin")
let regBtn=document.querySelector(".regBtn")
// 注册
regBtn.onclick =function(){
  telSelectItem.style.display = 'none';
  location.href="../html/reg.html"
}

let PW=document.querySelector(".userPW")
console.log(PW.value);
// 手机号正则判断
let isUserTel = false;
userTel.onchange = function () {
  console.log("ccc");
  let uTel = userTel.value;
  let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  if (reg.test(uTel)) {
    isUserTel = true;
  } else {
    alert("请检查手机号格式是否正确")
    isUserTel = false;
  }
}

let isPW = false;
PW.onchange = function () {
  let pw = this.value;
  let reg = /^\w{3,12}$/;
  if (reg.test(pw)) {
    isPW = true;
  } else {
    alert("密码只能0-12位的数字、字母、下划线")
    isPW = false;
  }
};

let logBtn = document.querySelector(".logBtn");
console.log(logBtn);
logBtn.onclick = async function () {
  telSelectItem.style.display = 'none';

// 登录按钮

  let username = userTel.value;
  let password = PW.value;

  if(isUserTel&&isPW){
    let obj=await axios.get(loginAPI, { params: { username, password } })
    console.log(obj);
    let autoLogin=document.querySelector(".autoLogin")
    if (obj.data.code == 1) {
      // 判断是否需要存储到本地
          if(autoLogin.checked){
            localStorage.setItem("id",obj.data.data.id)
            localStorage.setItem("token",obj.data.data.token)
            localStorage.setItem("username",obj.data.data.username)
            console.log(obj);
            location.href = "../index.html";
          }else{
            console.log("未选中");
            let cooObj={}
            cooObj.username = obj.data.data.username;
            cooObj.password = obj.data.data.password;
            cooObj.id = obj.data.data.id;

            console.log(cooObj);
            // 转为JSON字符串
            let str=JSON.stringify(cooObj);
            console.log(str); 
            // let time=0.0007  1秒
            let time=0.0049
            Cookies.set("user", str, { expires: time });
            Cookies.get("user")
            console.log(
              Cookies.get("user"));   
            location.href = "../index.html";
          }
      } else {
        alert("请检查用户名或者密码是否正确")
        location.href = "login.html";
          }
  }

  
}
let autolo=document.querySelector(".autlo")
autolo.onclick=function(){
  let res=autoLogin.checked==true?false:true;
  autoLogin.checked=res;
}
